package com.company.zhishidian.xuliehua;

import java.io.*;

/**
 * 对象序列化
 * 可以将一个对象持久化存储
 * 然后读取就可查看对象内容了
 */
public class Xuliehua {
    public static void main(String[] args) throws IOException {
        Student s = new Student("aaa",21,'男');
        ObjectOutputStream oos = new ObjectOutputStream(
                new FileOutputStream("pkg/dxu.txt")
        );
        oos.writeObject(s);
    }
}

class Test2{
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("pkg/dxu.txt"));
        Student s = (Student)ois.readObject();
        System.out.println(s);
    }
}

class Student implements Serializable {
    private String name;
    private int age;
    // transient 临时的意思，用在序列化当中，表示这个set不需要序列化
//    private transient char sex;
    private char sex;

    public Student(String name, int age, char sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Students{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                '}';
    }
}